package com.zzptc.shop.util;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MyUtil {
    /*
    * 读取配置文件XML
    * */
    public static Map<String,String > readXML()throws Exception{
        Map<String,String> map=new HashMap<String,String>();
        SAXBuilder saxBuilder=new SAXBuilder();
        String path=MyUtil.class.getResource("/").getPath()+"db-config.xml";
        //System.out.println(path);
        Document doc=saxBuilder.build(path);
        //得到根结点
        Element e=doc.getRootElement();
        System.out.println("根结点"+e.getName());
        //得到子结点
        List<Element> listall=e.getChildren();
        /*for(int i=0;i<listall.size();i++){
            System.out.println("子结点"+i+":"+listall.get(i).getName());
        }*/

        Iterator iter=listall.iterator();//拿到迭代器
        while (iter.hasNext()){//迭代器有几个方法  判断有没有元素hasNext()   有元素就next()
            Element a=(Element) iter.next();
            //System.out.println(a.getName()+"****"+a.getText());
            //放入map
            map.put(a.getName(),a.getText());
        }

        return map;
    }

    public static void main(String[] args) {
        try {
            System.out.println(MyUtil.readXML().size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
